package com.dailyblue.java.spring.boot.boot01.service.impl;

import com.dailyblue.java.spring.boot.boot01.bean.Emp;
import com.dailyblue.java.spring.boot.boot01.bean.JsonResult;
import com.dailyblue.java.spring.boot.boot01.mapper.EmpMapper;
import com.dailyblue.java.spring.boot.boot01.service.EmpService;
import com.dailyblue.java.spring.boot.boot01.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpMapper empMapper;
    @Override
    public JsonResult<List<Emp>> findAllEmp() {
        return new JsonResult<>(true,200,null,empMapper.findAllEmp());
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    public void deleteEmp(int id) {
        empMapper.deleteEmp(id);
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    public JsonResult<List<Emp>> saveEmp(Emp emp) {
        empMapper.saveEmp(emp);
        return new JsonResult<>(true,200,null,empMapper.findAllEmp());
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    public JsonResult<List<Emp>> updateEmp(Emp emp) {
        empMapper.updateEmp(emp);
        return ResultTool.success(findAllEmp());
    }

    @Override
    public JsonResult<PageInfo<Emp>> findEmpByPage(int page, int size) {
        PageHelper.startPage(page,size);
        List<Emp> list = empMapper.findAllEmp();
        PageInfo<Emp> pi = new PageInfo<>(list);
        return ResultTool.success(pi);
    }
}
